package com.zaimokuza.cn.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zaimokuza.cn.entity.PageResult;
import com.zaimokuza.cn.entity.blog.Comment;
import com.zaimokuza.cn.entity.constants.ActionConstant;
import com.zaimokuza.cn.entity.enums.CustomizeExceptionEnum;
import com.zaimokuza.cn.mapper.CommentMapper;
import com.zaimokuza.cn.service.ActionService;
import com.zaimokuza.cn.service.CommentService;
import com.zaimokuza.cn.service.LikeService;
import com.zaimokuza.cn.util.AssertUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * @author zaimokuza
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Resource
    private ActionService actionService;
    @Resource
    private LikeService likeService;
    @Resource
    private CommentMapper commentMapper;

    @Override
    public void addComment(Comment comment) {
        // 添加记录
        commentMapper.insertCommentRecord(comment);
        // 添加动态
        actionService.addAction(ActionConstant.TYPE_COMMENT, comment.getUserId(), comment.getBlogId(), comment.getId());
    }

    @Override
    public void deleteCommentForUser(String id, String userId) {
        // 检验是否有权限操作评论
        Integer commentIsExist = commentMapper.queryCommentIsExist(id, userId);
        AssertUtils.notNull(commentIsExist, CustomizeExceptionEnum.COMMENT_NOT_FOUND);
        // 删除评论
        deleteComment(id);
    }

    @Override
    public void deleteComment(String id) {
        // 删除评论及子评论
        commentMapper.deleteBatchCommentRecord(id);
        // 删除点赞记录
        likeService.deleteCommentLikeByCommentId(id);
        // 删除动态
        actionService.deleteAction(ActionConstant.TYPE_COMMENT, null, id);
    }

    @Override
    public void deleteCommentByBlog(String blogId) {
        // 根据博客ID批量删除评论
        commentMapper.deleteBatchCommentRecordByBlogId(blogId);
    }

    @Override
    public ArrayList<Comment> getCommentList(String blogId, String userId, String time) {
        // 查询基础评论列表
        ArrayList<Comment> commentList = commentMapper.queryCommentList(blogId, userId, time);
        // 如果无基础评论信息则直接返回
        if (commentList.size() == 0) {
            return commentList;
        }
        // 整理所有的基础评论ID作为父ID集合
        ArrayList<String> commentIdList = new ArrayList<>();
        for (Comment comment : commentList) {
            commentIdList.add(comment.getId());
        }
        // 根据父ID集合查询所有的子评论
        ArrayList<Comment> childCommentList = commentMapper.queryChildCommentList(commentIdList, userId);
        // 将子评论集合根据父ID进行整理
        HashMap<String, ArrayList<Comment>> tempMap = new HashMap<>(commentList.size());
        for (Comment childComment : childCommentList) {
            // 根据父ID整理子评论集合
            String parentId = childComment.getParentId();
            ArrayList<Comment> childList = tempMap.getOrDefault(parentId, new ArrayList<>());
            childList.add(childComment);
            tempMap.putIfAbsent(parentId, childList);
        }
        // 完善父评论信息
        for (Comment comment : commentList) {
            // 关联对应的子评论
            comment.setChildList(tempMap.get(comment.getId()));
        }
        // 返回结果
        return commentList;
    }

    @Override
    public PageResult<Comment> getCommentPage(String blogName, String userName, String content, Integer page, Integer size) {
        // 分页
        PageHelper.startPage(page, size);
        // 查询
        Page<Comment> commentPage = commentMapper.queryCommentPage(blogName, userName, content);
        // 封装结果并返回
        return PageResult.from(commentPage);
    }

    /* 统计相关操作 */

    @Override
    public void increaseCommentLikes(String commentId) {
        commentMapper.increaseCommentLikes(commentId);
    }

    @Override
    public void reduceCommentLikes(String commentId) {
        commentMapper.reduceCommentLikes(commentId);
    }

}
